﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;

namespace Clipping.SearchEnginePageParsers
{
	public class ZhongsouNewsPageParser : ISearchEnginePageParse
	{
		public void Parse(string html, out List<Infrastructure.Models.News> news, out string nextPageUrl)
		{
			news = new List<News>();
			nextPageUrl = null;
			HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
			doc.LoadHtml(html);
			var liNav = doc.CreateNavigator();


			//对全文搜索结果的提取
			var divSearchResult = liNav.Select("//div[@class='record_news']");

			while (divSearchResult.MoveNext())
			{
				News n = new News();

				var li = divSearchResult.Current;
				//取标题和链接
				var linkNode = li.SelectSingleNode("descendant::div[@class='t']/a");
				if (linkNode != null)
				{
					n.Title = System.Web.HttpUtility.HtmlDecode(linkNode.Value);
					n.Url = linkNode.GetAttribute("href", "");
				}

				//取媒体和时间
				var mediaAndTimeNode = li.SelectSingleNode("descendant::div[@class='t']");

				if (mediaAndTimeNode != null)
				{

					string str = mediaAndTimeNode.Value;
					Match match = Regex.Match(str, @"&nbsp;-&nbsp;(?<media>[^\s]+)\s+(?<date>[1-2][0-9]{3}/[0-2][0-9]/[0-3][0-9])\s+(?<time>[0-2][0-9]:[0-6][0-9])", RegexOptions.None);
					if (match.Success)
					{
						n.Media = System.Web.HttpUtility.HtmlDecode(match.Groups["media"].Value);
						n.PubDate = string.Format("{0} {1}", match.Groups["date"].Value, match.Groups["time"].Value);
					}


				}






				//取摘要
				var summaryNode = li.SelectSingleNode("descendant::div[@class='c']");
				if (summaryNode != null)
				{
					n.Summary = System.Web.HttpUtility.HtmlDecode(summaryNode.Value);
				}

				//取相同新闻
				var sameNewsUrlNode = li.SelectSingleNode("descendant::div[@class='m']/a");
				if (sameNewsUrlNode != null)
				{
					n.SameNewsUrl = System.Web.HttpUtility.HtmlDecode(sameNewsUrlNode.GetAttribute("href", ""));
				}
				news.Add(n);

			}


			//对标题搜索结果的提取
			divSearchResult = liNav.Select("//div[@class='record_news_more']");
			while (divSearchResult.MoveNext())
			{
				News n = new News();
				var li = divSearchResult.Current;
				//取标题和链接
				var linkNode = li.SelectSingleNode("descendant::a");
				if (linkNode != null)
				{
					n.Title = System.Web.HttpUtility.HtmlDecode(linkNode.Value);
					n.Url = System.Web.HttpUtility.UrlDecode(linkNode.GetAttribute("href", ""));
				}

				var mediaAndTimeNode = li.SelectSingleNode("descendant::span");
				Match match = Regex.Match(mediaAndTimeNode.Value, @"(?<media>[^\s]+)\s+(?<date>[1-2][0-9]{3}/[0-2][0-9]/[0-3][0-9])\s+(?<time>[0-2][0-9]:[0-6][0-9])", RegexOptions.None);
				if (match.Success)
				{
					n.Media = System.Web.HttpUtility.HtmlDecode(match.Groups["media"].Value);
					n.PubDate = string.Format("{0} {1}", match.Groups["date"].Value, match.Groups["time"].Value);
				}

				news.Add(n);
			}







			//取下一页地址
			var pageNodes = liNav.Select("//div[@class='page']/descendant::a");
			while (pageNodes.MoveNext())
			{
				var pageNode = pageNodes.Current;
				if (!string.IsNullOrEmpty(pageNode.Value) && pageNode.Value.Contains("下一页"))
				{
					nextPageUrl = pageNode.GetAttribute("href", "");
					break;
				}

			}


		}
	}
}
